package cn.Alibaba.system.controller;

import cn.Alibaba.basic.util.JsonResult;
import cn.Alibaba.basic.util.PageList;
import cn.Alibaba.system.domain.Dictionarydetail;
import cn.Alibaba.system.domain.Dictionarytype;
import cn.Alibaba.system.query.DictionarytypeQuery;
import cn.Alibaba.system.service.IDictionarydetailService;
import cn.Alibaba.system.service.IDictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：字典细节
 */
@RestController
@RequestMapping("/dictionarytype")
@Api(value = "字典细节的API",description="字典细节相关的CRUD功能") //打在Controller对Controller做描述
public class DictionarytypeController {

    @Autowired
    private IDictionarytypeService DictionarytypeService;

    @Autowired
    private IDictionarydetailService dictionarydetailService;

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}") // http://localhost:8080/Dictionarytype/1 + get
    public Dictionarytype findOne(@PathVariable("id") Long id) {
        return DictionarytypeService.findOne(id);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @ApiOperation(value = "查询所有")// 打在方法上，对方法做描述
    @GetMapping // http://localhost:8080/Dictionarytype + get  与上面的请求参数不同所以请求方式是不一样的
    public List<Dictionarytype> findAll() {

        return DictionarytypeService.findAll();
    }


    /**
     * 添加或修改
     *
     * @param Dictionarytype
     * @return
     */
    @ApiOperation(value = "添加或修改")// 打在方法上，对方法做描述
    @PutMapping // http://localhost:8080/Dictionarytype +JSon对象 + delete
    public JsonResult addOrUpdate(@RequestBody Dictionarytype Dictionarytype) { // 从请求体中获取JSON对象

        try {
            if (Dictionarytype.getId() == null) {
                DictionarytypeService.save(Dictionarytype);
            } else {
                DictionarytypeService.update(Dictionarytype);
            }

            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();

            return JsonResult.me().setMsg("操作失败");
        }
    }


    /**
     * 删除操作
     *
     * @param
     * @return
     */
    @ApiOperation(value = "删除操作")// 打在方法上，对方法做描述
    @DeleteMapping("/{id}") // http://localhost:8080/dictionarydetail + delete
    public JsonResult del(@PathVariable("id") Long id) {
        try {
            List<Dictionarydetail> all = dictionarydetailService.findAll();
            for (Dictionarydetail dictionarydetail : all) {
                if (dictionarydetail.getTypes_id() == id) {
                    dictionarydetailService.del(dictionarydetail.getId());
                }
            }

            DictionarytypeService.del(id);

            return JsonResult.me();


        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询")// 打在方法上，对方法做描述
    @PostMapping// http://localhost:8080/Dictionarytype +JSon对象 + post
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery query) {
        return DictionarytypeService.queryPage(query);
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @ApiOperation(value = "批量删除操作")// 打在方法上，对方法做描述
    @PatchMapping
    // 可以用数组 也可以用集合List<Long>
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
        /*List<DictionaryDetail> dictionaryDetails = dService.loadAll();
        for (DictionaryDetail dictionaryDetail : dictionaryDetails) {
            for (Long id : ids) {
                if (dictionaryDetail.getTypes_id()==id){
                    dService.remove(dictionaryDetail.getId());
                }
            }
        }*/
            for (Long id : ids) {
                List<Dictionarydetail> dictionaryDetails = dictionarydetailService.findAll();
                for (Dictionarydetail dictionaryDetail : dictionaryDetails) {
                    if (dictionaryDetail.getTypes_id() == id) {
                        dictionarydetailService.del(dictionaryDetail.getId());
                    }
                }
            }

            DictionarytypeService.PatchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
}


